<template>
  <div class="login_container" ref="vantaRef">
    <div class="login_box">
      <!-- LOGO区 -->
      <div class="avatar_box">
        <img
          src="https://tse4-mm.cn.bing.net/th/id/OIP-C.WPkvJjNXghayBVxzPKJYmQAAAA?w=198&h=199&c=7&r=0&o=5&pid=1.7"
          alt=""
        />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        :model="loginForm"
        :rules="loginRules"
        ref="loginFormRef"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="admin_phone">
          <el-input
            v-model="loginForm.admin_phone"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="admin_password">
          <el-input
            type="password"
            v-model="loginForm.admin_password"
            @keyup.enter.native="loginFormSubmit"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <!-- 登录按钮、重置按钮 -->
        <el-form-item class="btns">
          <el-button round @click="loginFormSubmit">登录</el-button>
          <el-button round @click="loginFormReset">用户登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { mapMutations } from "vuex";
import * as THREE from "three";
import fog from "vanta/src/vanta.fog";
import { Login } from "@/api/Login/index";
import axios from "axios";
export default {
  name: "loginForm",
  data() {
    return {
      // 表单数据绑定对象
      loginForm: {
        admin_phone: "",
        admin_password: "",
      },
      // 表单输入框验证规则
      loginRules: {
        admin_phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: "电话号码格式不对",
            trigger: "blur",
          },
        ],
        admin_password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 1,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // ...mapMutations(['UPDATEUSERINFO ']),
    loginFormReset() {
      // 重置表单调用
      this.$router.push("stulogin");
    },
    loginFormSubmit() {
      this.$refs.loginFormRef.validate(async (valid) => {
        // 判断是否要发起请求，如果为false代表表单验证不通过，不发起请求
        if (!valid) {
          return 0;
        } else {
          const { data: res } = await Login(
            this.loginForm.admin_phone,
            this.loginForm.admin_password
          );
          console.log("12333333333", this.loginForm);
          if (res.code === 0 && res.status !== false) {
            this.$store.dispatch("initUserInfo", this.loginForm.admin_phone);
            console.log("1233", res);
            this.$message.success("登录成功");
            this.$router.push('/main')
            // window.sessionStorage.setItem('token', res.data.accessToken)
            // 通过编程式导航跳转到后台主页，路由地址是home
          } else {
            this.$message.error(res.message);
          }
        }
      });
    },
  },
  mounted() {
    this.vantaEffect = fog({
      el: this.$refs.vantaRef,
      THREE: THREE,
    });
    this.vantaEffect.setOptions({
      mouseControls: true,
      touchControls: true,
      gyroControls: false,
      minHeight: 200.0,
      minWidth: 200.0,
      highlightColor: 0x54d7fa,
      midtoneColor: 0xbec9f5,
      lowlightColor: 0xededed,
      blurFactor: 0.55,
      speed: 2.7,
    });
  },
  beforeDestroy() {
    if (this.vantaEffect) {
      this.vantaEffect.destroy();
    }
  },
};
</script>
<style scoped>
.login_container {
  background-color: #ecf0f396;
  width: 100%;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  box-shadow: 10px 10px 10px #d1d9e67f, -10px -10px 10px #d1d9e67f;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #ecf0f396;
  box-shadow: 10px 10px 10px #d1d9e67f, -10px -10px 10px #d1d9e67f;
}

.avatar_box img {
  height: 100%;
  width: 100%;
  border-radius: 50%;
}

.login_form {
  position: absolute;
  bottom: 0px;
  width: 100%;
  padding: 0px 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
